package net.entelijan.cobean.core.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.entelijan.cobean.core.ICobean;

public abstract class AbstractBoundPropertyValueComponentBean<T> extends
		AbstractComponentBean implements ICobean<T> {

	private static Log log = LogFactory
			.getLog(AbstractBoundPropertyValueComponentBean.class);

	public AbstractBoundPropertyValueComponentBean() {
		super();
	}

	@Override
	public T getValue() {
		return getValuePlain();
	}

	protected abstract T getValuePlain();

	protected abstract void setValuePlain(T value);
	
	@Override
	public void setValue(T value) {
		T oldValue;
		try {
			oldValue = getValuePlain();
		} catch (Exception e) {
			if (log.isDebugEnabled()) {
				log.debug("Could not get old value of '" + this + "' because: "
						+ e.getMessage(), e);
			} else if (log.isInfoEnabled()) {
				log.info("Could not get old value of '" + this + "' because: "
						+ e.getMessage());
			} else {
				// No logging
			}
			oldValue = null;
		}
		setValuePlain(value);
		propertyChangeSupport.firePropertyChange("value", oldValue, value);
		log.debug("[" + getClass().getName() + "#setValue] value='" + value
				+ "' oldValue='" + oldValue + "'");
	}

}
